//
//  TweetDetailViewController.m
//  OSChina
//
//  Created by 8b410-2 on 17/1/1.
//  Copyright © 2017年 abb. All rights reserved.
//

#import "OSCTweetDetailViewController.h"
#import "OSCTweetDetailViewModel.h"
#import "OSCTweetCommentTableViewCell.h"
#import "OSCTweetTableViewCell.h"

static NSString* const reuseTweetCommentCell = @"TweetCommentCell";
static NSString* const reuseTweetDetailCell = @"TweetDetailCell";

@interface OSCTweetDetailViewController () <UITableViewDelegate,UITableViewDataSource>

/// 动弹详情viewModel
@property (nonatomic, strong, readonly)OSCTweetDetailViewModel *viewModel;

@end

@implementation OSCTweetDetailViewController
@dynamic viewModel;

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.tableView registerClass:[OSCTweetTableViewCell class] forCellReuseIdentifier:reuseTweetDetailCell];
    [self.tableView registerClass:[OSCTweetCommentTableViewCell class] forCellReuseIdentifier:reuseTweetCommentCell];
    [self setUpRefresh];
    [self setUpLoadMore];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.tableFooterView = [UIView new];
}

- (void)requestData {
    [self.viewModel.requestListDataCommand execute:nil];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 2;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (section == 0) {
        return 1;
    } else {
        return self.viewModel.dataSource.count;
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {// 动弹详情cell
        OSCTweetTableViewCell *tweetCell = [tableView dequeueReusableCellWithIdentifier:reuseTweetDetailCell forIndexPath:indexPath];
        [tweetCell bindViewModel:self.viewModel.tweetViewModel];
        return tweetCell;
    } else {// 动弹评论cell
        OSCTweetCommentTableViewCell *commentCell = [tableView dequeueReusableCellWithIdentifier:reuseTweetCommentCell forIndexPath:indexPath];
        [commentCell bindViewModel:self.viewModel.dataSource[indexPath.row]];
        return commentCell;
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        OSCTweetViewModel *tweetViewModel = self.viewModel.tweetViewModel;
        return [tableView fd_heightForCellWithIdentifier:reuseTweetDetailCell cacheByIndexPath:indexPath configuration:^(OSCTweetTableViewCell *cell) {
            [cell bindViewModel:tweetViewModel];
        }];
    } else {
        OSCTweetCommentViewModel *commentViewModel = self.viewModel.dataSource[indexPath.row];
        return [tableView fd_heightForCellWithIdentifier:reuseTweetCommentCell cacheByKey:@(commentViewModel.idField) configuration:^(OSCTweetCommentTableViewCell *cell) {
            [cell bindViewModel:commentViewModel];
        }];
    }
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    if (section == 0) {
        return nil;
    }
    UILabel *commentLabel = [UILabel labelWithSystemFontSize:15 textColor:[UIColor colorWithHex:0x909090]];
    [commentLabel setBackgroundColor:[UIColor titleBarColor]];
    commentLabel.textAlignment = NSTextAlignmentCenter;
    commentLabel.text = @"评论";
    return commentLabel;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    if (section == 0) {
        return 0.00f;
    }
    return 36;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    if (indexPath.section == 0) {
        return;
    }
    OSCTweetCommentViewModel *commentViewModel = self.viewModel.dataSource[indexPath.row];
    [self.viewModel.didSelectCommand execute:commentViewModel];
}
@end
